
from flask import Flask
from flask import jsonify
import mdmanager
import thread
import logging
from flask import request
import  importlib
app = Flask(__name__)
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',filename='flk.log')
logger = logging.getLogger('main')

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/<module>/<func>',methods = ['GET','POST'])
def call(module, func):
    try:
        para = request.get_json()
        logger.info('module is %s, function is %s, para is %s' % (module, func, para))
        m = importlib.import_module('.'+module,'libs')
        f = getattr(m, func)
        call_result = f(para)
        return jsonify(call_result)
    except Exception as e:
        logger.error(e)
        return jsonify(e)

if __name__ == '__main__':
    #app.run(host='0.0.0.0')
    thread.start_new_thread(mdmanager.watch,('./libs',))
    app.run()

